﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core;
using DAL.Help;
using System.Data;
using DAL.Connect;

namespace DAL.Classes
{
    public class BaiDang
    {
        #region khai bao cac thuoc tinh anh xa
        public int MaBaiDang { get; set; }
        public string TenBaiDang { get; set; }
        public DateTime NgayTao { get; set; }
        public string MoTa { get; set; }
        public string NoiDung { get; set; }
        public int MaTheLoai { get; set; }
        public int MaChuDe { get; set; }
        public int MaTaiKhoan { get; set; }
        public int TrangThai { get; set; }
        public string TenDangNhap { get; set; }
        #endregion

        #region Cac phuong thuc them xoa sua
        public static bool Them(BaiDang baidang)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQueryWithOutput("@MaBaiDang", "BaiDang_Them",
                    baidang.MaBaiDang, baidang.TenBaiDang, baidang.NgayTao, baidang.MoTa, baidang.NoiDung, baidang.MaTheLoai, baidang.MaChuDe, baidang.MaTaiKhoan, baidang.TrangThai);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }

        }

        public static bool Sua(BaiDang baidang)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("BaiDang_Sua", baidang.MaBaiDang, baidang.TenBaiDang, baidang.MoTa);
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool SuaTrangThai(string maBaiDang, string trangThai)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("BaiDang_SuaTrangThai", Convert.ToInt32(maBaiDang), Convert.ToInt32(trangThai));
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        public static bool Xoa(string maBaiDang)
        {
            try
            {
                object rs = DataProvider.Instance.ExecuteNonQuery("BaiDang_Xoa", Convert.ToInt32(maBaiDang));
                return Convert.ToInt32(rs) > 0;
            }
            catch
            {
                return false;
            }
        }
        #endregion

        #region Cac phuong thuc lay du lieu
        public static BaiDang LayMaBaiDangMax()
        {
            try
            {
                return CBO.FillObject<BaiDang>(DataProvider.Instance.ExecuteReader("BaiDang_MaBaiDangMax"));
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static BaiDang LayTheoMa(string maBaiDang)
        {
            try
            {
                return CBO.FillObject<BaiDang>(DataProvider.Instance.ExecuteReader("BaiDang_LayTheoMa", Convert.ToInt32(maBaiDang)));
            }
            catch
            {
                return null;
            }
        }
        public static BaiDang LayThongTinTheoMaBaiDang(string maBaiDang)
        {
            try
            {
                return CBO.FillObject<BaiDang>(DataProvider.Instance.ExecuteReader("BaiDang_LayThongTinTheoMaBaiDang",
                                                Convert.ToInt32(maBaiDang)));
            }
            catch
            {
                return null;
            }
        }
        public static BaiDang LayMaTaiKhoanTheoBaiDang(string maBaiDang)
        {
            try
            {
                return CBO.FillObject<BaiDang>(DataProvider.Instance.ExecuteReader("BaiDang_LayMaTaiKhoanTheoBaiDang", maBaiDang));
            }
            catch (Exception)
            {
                return null;
            }
        }
        public static int DemBaiDangTheoMaTaiKhoan_TheLoai(string maTaiKhoan, string maTheLoai)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("BaiDang_DemBaiDangTheoTaiKhoanVaTheLoai", ConvertType.ToInt32(maTaiKhoan), ConvertType.ToInt32(maTheLoai)));
            }
            catch
            {
                return 0;
            }
        }
        public static int DemBaiDangTheoChuDeVaTheLoai(string maChuDe, string maTheLoai)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("BaiDang_DemBaiDangTheoChuDeVaTheLoai", ConvertType.ToInt32(maChuDe), ConvertType.ToInt32(maTheLoai)));
            }
            catch
            {
                return 0;
            }
        }
        public static int DemBaiDangTheoKetQuaTimKiem(string theLoai, string search)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("BaiDang_DemBaiDangTheoKetQuaTimKiem", theLoai, search));
            }
            catch
            {
                return 0;
            }
        }
        public static int DemBaiDangTheoTrangThai(string trangThai)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("BaiDang_DemBaiDangTheoTrangThai", ConvertType.ToInt32(trangThai)));
            }
            catch
            {
                return 0;
            }
        }
        public static int DemBaiDangTheoTheLoai(string theLoai)
        {
            try
            {
                return Convert.ToInt32(DataProvider.Instance.ExecuteScalar("BaiDang_DemBaiDangTheoTheLoai", ConvertType.ToInt32(theLoai)));
            }
            catch
            {
                return 0;
            }
        }
        public static List<BaiDang> BaiDangTop10TheoTheLoai(string theLoai)
        {
            try
            {
                return CBO.FillCollection<BaiDang>(DataProvider.Instance.ExecuteReader("BaiDang_Top10TheoTheLoai", ConvertType.ToInt32(theLoai)));
            }
            catch
            {
                return null;
            }
        }
        public static List<BaiDang> BaiDangThuocChuDe_TheLoai(string maChuDe, string maTheLoai, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiDang_LayTheoChuDeVaTheLoai_PT", Convert.ToInt32(maChuDe), Convert.ToInt32(maTheLoai),
                                                            GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                            pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiDang>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0; return new List<BaiDang>();
            }
        }
        public static List<BaiDang> BaiDangTheoTaiKhoan_TheLoai(string maTaiKhoan, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiDang_LayTheoTaiKhoan_PT", Convert.ToInt32(maTaiKhoan),
                                                            GlobalConfiguration.DescriptionLength, Convert.ToInt32(page),
                                                            pageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiDang>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false) reader.Close();
                howManyPages = 0; return new List<BaiDang>();
            }
        }
        public static List<BaiDang> TimTheoTenVaTheLoai(string maTheLoai, string searchString, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiDang_TimKiemTheoTenVaTheLoai_PT", maTheLoai, searchString, GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiDang>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiDang>();
            }
        }
        public static List<BaiDang> LayTheoTheLoai_PT(string maTheLoai, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiDang_LayTheoTheLoai_PT", maTheLoai, GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiDang>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiDang>();
            }
        }
        public static List<BaiDang> LayTheoTrangThai_PT(string trangThai, string page, out int howManyPages)
        {
            IDataReader reader = null;
            try
            {
                int pageSize = GlobalConfiguration.PageSize;
                reader = DataProvider.Instance.ExecuteReader("BaiDang_LayTheoTrangThai_PT", trangThai, GlobalConfiguration.DescriptionLength, page, GlobalConfiguration.PageSize);
                reader.Read();
                howManyPages = (int)Math.Ceiling((double)reader.GetInt32(0) / (double)pageSize);
                reader.NextResult();
                return CBO.FillCollection<BaiDang>(reader);
            }
            catch
            {
                if (reader != null && reader.IsClosed == false)
                    reader.Close();
                howManyPages = 0;
                return new List<BaiDang>();
            }
        }
        #endregion
    }
}
